package control;


import rabid.Rabid;
import ui.FileDisplay;
import ui.RABIDUI;

/**
 * @author Angela
 * This class will serve as the link between the UI and the backend.
 * This class has been created in case there needs to be a command
 * line version of Rabid and to provide more flexibility with respect
 * to the UI
 */
public class RabidController {

	//the user interface
	private RABIDUI uinterface;
	
	//the query engine
	private Rabid engine;
	
	
	public RabidController() {
		uinterface = new RABIDUI(this);
		
		try {
			engine = new Rabid();
		} catch (Exception e) {
			uinterface.inform(Constants.IndexError, true);
		} 
	}
	
	public void fetchCommand(String command) {
		
		if(command.equals(Constants.C_STATS)) {
			String stats = engine.getIndex().getStats();
			uinterface.setDisplayContent(stats);
		
		} else if(command.equals(Constants.C_INDEX)) {
			String index = engine.getIndex().getIndexInfo();
			uinterface.setDisplayContent(index);
		
		} else if(command.equals(Constants.C_QUERY)) {
			String[] query = uinterface.getQueryString();
			String[] files = engine.query(query);
			uinterface.setResultPane(files);
		
		} else if(command.equals(Constants.C_DISPLAY)) {
			String filePath = engine.getIndex().getFilePath(
					uinterface.getSelectedFile());
			new FileDisplay(filePath);
		
		} else if(command.equals(Constants.C_SIMILARTO)) {
			int fileIndex = engine.getIndex().getFileIndex(
					uinterface.getSelectedFile());
			if(fileIndex > 0) {
				String[] files = engine.similarTo(fileIndex);
				uinterface.setResultPane(files);
			}
		
		} else if(command.equals(Constants.C_BROWSE)) {
			System.out.println("Browse selected");
			String[] browse = uinterface.getQueryString();
			String[] results = engine.browse(browse);
			uinterface.setResultPane(results);
		}
		
	}//end fetch command
	
	public static void main(String[] args) {
		new RabidController();
	}
}
